package net.nshc.kwkim.mycalc.db;

import net.nshc.kwkim.mycalc.*;
import net.nshc.kwkim.mycalc.activity.*;
import net.nshc.kwkim.mycalc.db.DataBases.CreateDB;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.text.format.*;
import android.util.*;

public class DBOpenHelper {
	public static SQLiteDatabase sdb;
	private DatabaseHelper DBHelper;
	private Context context;

	private class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, null, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db){
			String sql = "CREATE TABLE IF NOT EXISTS " + CreateDB.TABLE_NAME + "( _id INTEGER PRIMARY KEY AUTOINCREMENT, "+ CreateDB.RESULT + " text not null, "
					+CreateDB.LOC + " text null, "
					+CreateDB.TIME + " date not null);";
			db.execSQL(sql);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
			if(MyCalcActivity.bLog)Log.w(LogDefine.LOG_DATABASE, "Version mismatch : " + oldVersion + "to" + newVersion);
			db.execSQL("DROP TABLE " + CreateDB.TABLE_NAME);
			onCreate(db);
		}
	}

	public DBOpenHelper(Context context) {
		this.context = context;
	}
	
	public DBOpenHelper open() throws SQLException{
		DBHelper = new DatabaseHelper(context, DataBases.CreateDB.DB_NAME, null, DataBases.CreateDB.DB_VERSION);
		sdb = DBHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
//		DBHelper.close();
		sdb.close();
	}
	
	public long insertColumn(String result, String location){
		ContentValues values = new ContentValues();
		values.put(DataBases.CreateDB.RESULT, result);
		values.put(DataBases.CreateDB.LOC, location);
		values.put(DataBases.CreateDB.TIME, DateFormat.format("yyyy-MM-dd hh:mm:ss", System.currentTimeMillis()).toString());
		if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_DATABASE,"DBOpenHelper_result = "+result+", location = "+location);
		return sdb.insert(DataBases.CreateDB.TABLE_NAME, null, values);
	}

	// Update DB
	public boolean updateColumn(long id , String result, String location){
		ContentValues values = new ContentValues();
		values.put(DataBases.CreateDB.RESULT, result);
		values.put(DataBases.CreateDB.LOC, location);
		values.put(DataBases.CreateDB.TIME, DateFormat.format("yyyy-MM-dd hh:mm:ss", System.currentTimeMillis()).toString());
		return sdb.update(DataBases.CreateDB.TABLE_NAME, values, "_id="+id, null) > 0;
	}

	// Delete ID
	public boolean deleteColumn(long id){
		return sdb.delete(DataBases.CreateDB.TABLE_NAME, "_id="+id, null) > 0;
	}
	
	public boolean deleteAllColumn(){
		return sdb.delete(DataBases.CreateDB.TABLE_NAME, null, null) > 0;
	}
	
	// Select All
	public Cursor getAllColumns(){
		return sdb.query(DataBases.CreateDB.TABLE_NAME, null, null, null, null, null, null);
	}

	// ID 컬럼 얻어 오기
	public Cursor getColumn(long id){
		Cursor c = sdb.query(DataBases.CreateDB.TABLE_NAME, null, 
				"_id="+id, null, null, null, null);
		if(c != null && c.getCount() != 0)
			c.moveToFirst();
		return c;
	}

	// 이름 검색 하기 (rawQuery)
	public Cursor getMatchName(String result){
		Cursor c = sdb.rawQuery( "select * from history where result=" + "'" + result + "'" , null);
		return c;
	}
}
